import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Req,
  UseInterceptors,
  ClassSerializerInterceptor,
  Query,
  Res,
  UploadedFile,
} from '@nestjs/common';
import { UserService } from './user.service';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto, resetPwdDto } from './dto/update-user.dto';
import { updateProfileDto, updatePwdDto } from './dto/update-profile.dto';
import { User } from './entities/user.entity';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiBearerAuth,
} from '@nestjs/swagger';
import { PipePipe } from './pipe/pipe.pipe';
import { Public } from 'src/decorator/public';
import { Request, Response } from 'express';
import { QueryUserDto } from './dto/query-user.dto';
import { DeleteUserDto } from './dto/delete-user.dto';
import { StatusChangeDto } from 'src/common/baseDto/status-change.dto';
import { RequirePermission } from 'src/decorator/reuqire-permission';
import { FileInterceptor } from '@nestjs/platform-express';

@ApiTags('用户管理')
@ApiBearerAuth()
@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) {}

  @Post('register')
  @Public()
  @UseInterceptors(ClassSerializerInterceptor)
  @ApiOperation({ summary: '注册用户' })
  @ApiResponse({ status: 201, type: [User] })
  create(@Body(PipePipe) createUserDto: CreateUserDto) {
    return this.userService.create(createUserDto);
  }

  @Post('add')
  @UseInterceptors(ClassSerializerInterceptor)
  @ApiOperation({ summary: '添加用户' })
  @RequirePermission('system:user:add')
  add(@Body(PipePipe) params: CreateUserDto, @Req() req: Request) {
    return this.userService.addUser(params, req);
  }

  @Get('list')
  @ApiOperation({ summary: '获取用户列表' })
  @RequirePermission('system:user:list')
  findAll(@Query() query: QueryUserDto) {
    return this.userService.findAll(query);
  }

  @Get('info/:id')
  @ApiOperation({ summary: '获取单个用户详情' })
  findOne(@Param('id') id: string) {
    return this.userService.findOne(+id);
  }

  @Patch('info/:id')
  @ApiOperation({ summary: '更新用户信息' })
  @RequirePermission('system:user:edit')
  update(
    @Param('id') id: string,
    @Body() updateUserDto: UpdateUserDto,
    @Req() req: Request,
  ) {
    return this.userService.update(+id, updateUserDto, req);
  }

  @Post('statusChange')
  @ApiOperation({ summary: '更新用户状态' })
  @RequirePermission('system:user:edit')
  statusChange(@Body() statusChangeDto: StatusChangeDto, @Req() req: Request) {
    return this.userService.statusChange(statusChangeDto, req);
  }

  @Post('delete')
  @ApiOperation({ summary: '删除用户' })
  @RequirePermission('system:user:remove')
  remove(@Body() dto: DeleteUserDto) {
    return this.userService.remove(dto.userIds);
  }

  @Post('excel')
  @ApiOperation({ summary: '导出用户' })
  @RequirePermission('system:user:export')
  exportToExcel(@Res() res: Response) {
    return this.userService.exportToExcel(res);
  }

  @Post('importData')
  @ApiOperation({ summary: '导入用户' })
  @UseInterceptors(FileInterceptor('file')) //配置文件拦截器  file 为上传文件的key
  @RequirePermission('system:user:import')
  importData(
    @Query('updateSupport') updateSupport: string,
    @UploadedFile() file: Express.Multer.File,
  ) {
    return this.userService.importData(updateSupport, file);
  }

  @Post('resetPwd')
  @ApiOperation({ summary: '重置密码' })
  @RequirePermission('system:user:resetPwd')
  resetPwd(@Body() dto: resetPwdDto) {
    return this.userService.resetPwd(dto);
  }

  @Post('importTemplate')
  @ApiOperation({ summary: '下载模版' })
  importTemplate(@Res() res: Response) {
    return this.userService.importTemplate(res);
  }

  @Get('profile')
  @ApiOperation({ summary: '查询用户个人信息' })
  getUserProfile(@Req() req: Request) {
    return this.userService.getUserProfile(req);
  }

  @Post('profile')
  @ApiOperation({ summary: '修改用户个人信息' })
  updateUserProfile(
    @Body() updateProfileDto: updateProfileDto,
    @Req() req: Request,
  ) {
    console.log("🚀 ~ UserController ~ 修改用户个人信息:")
    
    return this.userService.updateUserProfile(updateProfileDto, req);
  }

  @Post('profile/updatePwd')
  @ApiOperation({ summary: '用户密码重置' })
  updatePwd(@Query() updatePwdDto: updatePwdDto, @Req() req: Request) {
    return this.userService.updatePwd(updatePwdDto, req);
  }

  @Post('profile/avatar')
  @ApiOperation({ summary: '用户头像上传' })
  @UseInterceptors(FileInterceptor('file')) //配置文件拦截器  file 为上传文件的key
  uploadAvatar(@UploadedFile() file: Express.Multer.File,@Req() req:Request) {
    console.log("🚀 ~ UserController ~ uploadAvatar ~ file:", file)
    return this.userService.uploadAvatar(file,req);
  }
}
